using UnityEngine;
using QFramework;

namespace QFrameworkSurvivor
{
	public partial class CameraController : ViewController
	{
        private static CameraController mDefault=null;
        public static Transform LBTran => mDefault.LB;
        public static Transform RTTran => mDefault.RT;
        private void Awake()
        {
            mDefault = this;
        }
        private Vector2 mTargetPosition = Vector2.zero;
        private Vector3 CurrentCameraPos;
        private bool mShake = false;
        private int mShakeFrame= 0;
        private float mShakeA=0.5f;
        public static void Shake()
        {
            mDefault.mShake = true;
            mDefault.mShakeFrame = 100;
        }
        private void Update()
        {
            if(Player.Default)
            {
                mTargetPosition = (Vector2)Player.Default.transform.position;
                mTargetPosition.x = (1.0f - Mathf.Exp(-Time.deltaTime * 20)).Lerp(transform.position.x, mTargetPosition.x);
                mTargetPosition.y = (1.0f - Mathf.Exp(-Time.deltaTime * 20)).Lerp(transform.position.y, mTargetPosition.y);
                if (mShake)
                {
                    mShakeFrame--;
                    if(mShakeFrame%5==0)
                    {
                        var shakeA = Mathf.Lerp(mShakeA, 0.0f, mShakeFrame / 100.0f);
                        CurrentCameraPos = transform.position;
                        transform.position = new Vector3(CurrentCameraPos.x + Random.Range(-shakeA, shakeA), CurrentCameraPos.y + Random.Range(-shakeA, shakeA), CurrentCameraPos.z);
                    }
                    if(mShakeFrame <= 0)
                    {
                        mShake = false;
                        mShakeFrame = 100;
                    }
                }
                else
                {
                    transform.PositionX((1.0f - Mathf.Exp(-Time.deltaTime * 20)).Lerp(transform.position.x, mTargetPosition.x));
                    transform.PositionY((1.0f - Mathf.Exp(-Time.deltaTime * 20)).Lerp(transform.position.y, mTargetPosition.y));
                }
            }
        }
        private void OnDestroy()
        {
            mDefault = null;
        }
    }
}
